﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Reflection
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Serialization
Imports System.Xml

Namespace System.Workflow.ComponentModel.Design
    Public Class ActivityDesignerLayoutSerializer
        Inherits WorkflowMarkupSerializer
        ' Methods
        Protected Overrides Function CreateInstance(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal type As Type) As Object
            Dim activity3 As Activity
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (type Is Nothing) Then
                Throw New ArgumentNullException("type")
            End If
            Dim obj2 As Object = Nothing
            Dim service As IDesignerHost = [TryCast](serializationManager.GetService(GetType(IDesignerHost)),IDesignerHost)
            Dim reader As XmlReader = [TryCast](serializationManager.WorkflowMarkupStack.Item(GetType(XmlReader)),XmlReader)
            If ((service Is Nothing) OrElse (reader Is Nothing)) Then
                Return obj2
            End If
            Dim str As String = String.Empty
            Do While (reader.MoveToNextAttribute AndAlso Not reader.LocalName.Equals("Name", StringComparison.Ordinal))
            Loop
            If (reader.LocalName.Equals("Name", StringComparison.Ordinal) AndAlso reader.ReadAttributeValue) Then
                str = reader.Value
            End If
            reader.MoveToElement
            If String.IsNullOrEmpty(str) Then
                serializationManager.ReportError(SR.GetString("Error_LayoutSerializationAssociatedActivityNotFound", New Object() { reader.LocalName, "Name" }))
                Return obj2
            End If
            Dim designer As CompositeActivityDesigner = [TryCast](serializationManager.Context.Item(GetType(CompositeActivityDesigner)),CompositeActivityDesigner)
            If (Not designer Is Nothing) Then
                Dim activity2 As CompositeActivity = [TryCast](designer.Activity,CompositeActivity)
                If (activity2 Is Nothing) Then
                    goto Label_01CA
                End If
                activity3 = Nothing
                Dim activity4 As Activity
                For Each activity4 In activity2.Activities
                    If str.Equals(activity4.Name, StringComparison.Ordinal) Then
                        activity3 = activity4
                        Exit For
                    End If
                Next
            Else
                Dim rootComponent As Activity = [TryCast](service.RootComponent,Activity)
                If ((Not rootComponent Is Nothing) AndAlso Not str.Equals(rootComponent.Name, StringComparison.Ordinal)) Then
                    Dim component As IComponent
                    For Each component In service.Container.Components
                        rootComponent = [TryCast](component,Activity)
                        If ((Not rootComponent Is Nothing) AndAlso str.Equals(rootComponent.Name, StringComparison.Ordinal)) Then
                            Exit For
                        End If
                    Next
                End If
                If (Not rootComponent Is Nothing) Then
                    obj2 = service.GetDesigner(rootComponent)
                End If
                goto Label_01CA
            End If
            If (Not activity3 Is Nothing) Then
                obj2 = service.GetDesigner(activity3)
            End If
        Label_01CA:
            If (obj2 Is Nothing) Then
                serializationManager.ReportError(SR.GetString("Error_LayoutSerializationActivityNotFound", New Object() { reader.LocalName, str, "Name" }))
            End If
            Return obj2
        End Function

        Protected Friend Overrides Function GetProperties(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object) As PropertyInfo()
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            Dim list As New List([Of] PropertyInfo)(MyBase.GetProperties(serializationManager, obj))
            Dim designer As ActivityDesigner = [TryCast](obj,ActivityDesigner)
            If (Not designer Is Nothing) Then
                Dim property As PropertyInfo = designer.GetType.GetProperty("Name", (BindingFlags.NonPublic Or BindingFlags.Instance))
                If (Not [property] Is Nothing) Then
                    list.Insert(0, [property])
                End If
            End If
            Return list.ToArray
        End Function

        Protected Overrides Sub OnBeforeSerialize(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
            MyBase.OnBeforeSerialize(serializationManager, obj)
            Dim designer As ActivityDesigner = [TryCast](obj,ActivityDesigner)
            Dim writer As XmlWriter = [TryCast](serializationManager.WorkflowMarkupStack.Item(GetType(XmlWriter)),XmlWriter)
            If (((Not designer.Activity Is Nothing) AndAlso (designer.Activity.Parent Is Nothing)) AndAlso (Not writer Is Nothing)) Then
                Dim prefix As String = String.Empty
                Dim xmlQualifiedName As XmlQualifiedName = serializationManager.GetXmlQualifiedName(GetType(Point), prefix)
                writer.WriteAttributeString("xmlns", prefix, Nothing, xmlQualifiedName.Namespace)
            End If
        End Sub

    End Class
End Namespace

